package com.asialjim.wechat.official.qrcode;

import com.asialjim.wechat.apiservice.constant.WeChatConstants;
import com.asialjim.wechat.official.api.qrcode.GetQrCodeTicketApiResponse;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.net.URLEncoder;

/**
 * <h1><em>ASIAL JIM JAVA DOC</em></h1><hr/>
 * <h2>CLASS DESCRIPTION <i>[ NAME: QrCode ]</i></h2><strong>
 * <p>
 * </strong><p><p><em>Copyright &copy; Asial Jim Co., LTD</em><hr/>
 *
 * @author Asial Jim &nbsp;&nbsp; <span>Email:<a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
 * @version 1.0.0
 * @since 2021/3/31   &nbsp;&nbsp; JDK 8
 */
@Data
@NoArgsConstructor
@AllArgsConstructor
public class QrCode implements Serializable {
    private static final long serialVersionUID = -8619383854167411715L;

    /**
     * <h3>FIELD DESCRIPTION</h3>
     * 二维码图片解析后的地址，开发者可根据该地址自行生成需要的二维码图片
     */
    private String url;

    /**
     * <h3>FIELD DESCRIPTION</h3>
     * 获取二维码ticket后，开发者可用ticket换取二维码图片
     */
    private String ticketUrl;

    /**
     * <h3>METHOD DESCRIPTION <i>[ NAME: createQrCode ]</i></h3>
     * get qr code by qr code ticket api resposne
     * @param response {@link GetQrCodeTicketApiResponse api resposne}
     * @return {@link QrCode qr code info}
     * @author Asial Jim &nbsp;&nbsp; <span>Email: &nbsp;&nbsp; <a href="mailto:asialjim@hotmail.com">asialjim@hotmail.com</a> &nbsp;&nbsp; <a href="asialjim@qq.com">asialjim@qq.com</a></span>
     * @since 2021/3/31 17:17
     */
    public static QrCode createQrCode(GetQrCodeTicketApiResponse response){
        String ticket = URLEncoder.encode(response.getTicket());
        String ticketUrl = String.format(WeChatConstants.WeChatApiUrl.GET_QRCODE_BY_COMMON_DOMAIN, ticket);
        return new QrCode(response.getUrl(),ticketUrl);
    }
}